//
//  BSStatusOriginalView.swift
//  mySwiftWeibo
//
//  Created by apple on 16/7/29.
//  Copyright © 2016年 itcast沈庆伟. All rights reserved.
//

import UIKit

//原创view,显示原创的信息
class BSStatusOriginalView: UIView {
    
    ///微博模型
    var status: BSStatus?{
        didSet{
            
            //头像
            if let profile_image_url = status?.user?.profile_image_url{
                let url = NSURL(string: profile_image_url)
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            
            //名称
            nameLabel.text = status?.user?.screen_name
            
            //时间
            timeLabel.text=status?.created_at
            
            //来源
            sourceLabel.text=status?.source
            
            //会员等级
            if let mbrank = status?.user?.mbrank{
                if mbrank > 0 && mbrank <= 6{
                    let image = UIImage(named: "common_icon_membership_level\(mbrank)")
                    memberImageView.image = image
                }else{
                    memberImageView.image=nil
                }
            }
            
            //认证类型
            if let verified_type = status?.user?.verified_type{
                var image: UIImage? = nil
                
                switch verified_type{
                case 0:
                    //认证用户
                    image = UIImage(named: "avatar_vip")
                case 2,3,5:
                    image = UIImage(named: "avatar_enterprise_vip")
                case 200:
                    image = UIImage(named: "avatar_grassroot")
                default:
                    image = nil
                }
                verifiedImageView.image=image
            }
            
            //微博内容
            contentLabel.text=status?.text
            
            //将原创view的图片url设置给配图视图
            pictureView.imageURLs = status?.storePictureURLs
            
            //当没有配图的时候 pictureview size = 0,配图顶部到到contentLabel底部的间距=0
            pictureView.snp_makeConstraints { (make) -> Void in
                var margin: CGFloat = 0
                if status?.storePictureURLs?.count > 0 {
                    margin = CellMargin
                }
                
                //没有配图
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        
        setupUI()
    }

    ///设置UI
    private func setupUI(){
        
        //添加
        self.addSubview(iconView)
        self.addSubview(nameLabel)
        self.addSubview(timeLabel)
        self.addSubview(sourceLabel)
        self.addSubview(memberImageView)
        self.addSubview(verifiedImageView)
        self.addSubview(contentLabel)
        self.addSubview(pictureView)
        
        //约束
        //头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(CellMargin)
            make.size.equalTo(CGSize(width: CellIconWH, height: CellIconWH))
        }
        
        //名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(CellMargin)
            make.top.equalTo(iconView)
        }
        
        //时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(iconView)
        }
        
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(CellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        //会员等级
        memberImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(CellMargin)
            make.centerY.equalTo(nameLabel)
        }
        
        //认证图标
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        //微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(12)
        }
        
        //配图视图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(CellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(CellMargin)
            
            //测试大小,其实应该根据图片的数量计算
            make.size.equalTo(CGSize(width: 359, height: 359))
        }
        
        //约束originalView的底部和 微博文本配图的底部重合
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView).offset(CellMargin)
        }
        
    }
    
    //MARK: - lanjiazai 
    ///用户头像
    private lazy var iconView: UIImageView=UIImageView(image: UIImage(named: "avatar"))
    
    ///用户名称
    private lazy var nameLabel: UILabel={
        let label=UILabel()
        
        label.textColor=UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(15)
        label.text="老王"
        label.sizeToFit()
        
        return label
    }()
    
    ///微博发布时间
    private lazy var timeLabel: UILabel={
        let label=UILabel()
        
        label.textColor=UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(12)
        label.text="刚刚"
        label.sizeToFit()
       
        return label
    }()
    
    ///微博来源
    private lazy var sourceLabel: UILabel={
        let label=UILabel()
        
        label.textColor=UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(12)
        label.text="来自..."
        label.sizeToFit()
        
        return label
    }()
    
    ///会员等级
    private lazy var memberImageView: UIImageView=UIImageView(image: UIImage(named: "common_icon_membership"))
    
    ///认证图标
    private lazy var verifiedImageView: UIImageView=UIImageView(image: UIImage(named: "avatar_vip"))
    
    ///微博内容
    private lazy var contentLabel: UILabel={
        let label=UILabel(textColor: UIColor.darkGrayColor(), fontSize: 16)
       
        label.text="微博内容"
        
        //多行
        label.numberOfLines = 0
        //文字换行宽度
        label.preferredMaxLayoutWidth = UIScreen.width() - 2 * CellMargin
        label.sizeToFit()
        
        return label
    }()
    
    ///配图视图
    private lazy var pictureView: BSStatusPictureView=BSStatusPictureView()
}
